<template>
  <el-cascader
    :value="value"
    :options="treeData"
    :props="props"
    separator="-"
    @change="changeValue"
  />
</template>

<script>
import { getDepartmentApi } from '@/api/department'
import { transObjToTree } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      props: {
        label: 'name',
        value: 'id'
      },
      treeData: []
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      console.log(transObjToTree(await getDepartmentApi(), 0))
      this.treeData = transObjToTree(await getDepartmentApi(), 0)
    },
    changeValue(list) {
      if (list.length) {
        // list[list.length - 1] 这个拿到的是最后一个部门的id
        this.$emit('input', list[list.length - 1])
        console.log(list)
      } else {
        this.$emit('input', null)
      }
    }
  }
}

</script>
